<?php

namespace app\controller\system;

use app\controller\BaseController;
use app\model\system\SysUser;
use app\service\Logger;
use support\Request;
use support\Response;

class UserController extends BaseController
{

    public function me(Request $request): Response
    {
        $jwtUser = $request->currUser; //userName,userId
        $user = SysUser::where('id', $jwtUser->userId)->find();
        if (!$user) {
            return $this->error('数据不存在');
        }
        return $this->success($user->toArray());
    }

    public function create(Request $request): Response
    {
        $data = $request->post();
        $user = SysUser::create($data);
        if (!$user) {
            return $this->error('创建失败');
        } else {
            return $this->success([]);
        }
    }

    public function get(int $id): Response
    {
        $log = SysUser::where('id', $id)->find();
        if (!$log) {
            return $this->error('数据不存在');
        }
        return $this->success($log->getFormArray());
    }


    public function update(Request $request, int $id): Response
    {
        $data = $request->post();
        $user = SysUser::find($id);

        if (!$user) {
            return $this->error('数据不存在');
        }

        if ($user->update($data)) {
            return $this->success($user->getFormArray());
        } else {
            return $this->error('更新失败');
        }
    }

    public function delete(int $id): Response
    {
        $user = SysUser::find($id);
        if (!$user) {
            return $this->error('数据不存在');
        }
        if (!$user->save(["status" => 0, "is_deleted" => 1])) {
            return $this->error('删除失败');
        } else {
            return $this->success(["id" => $id]);
        }
    }

    public function page(Request $request): Response
    {
        $params = $request->get();
        extract($params);
        $userModel = SysUser::order("id", "desc");
        if (isset($deptId) && $deptId) {
            $userModel->where("dept_id", $deptId);
        }
        if (isset($status)) {
            $userModel->where("status", $status);
        }
        if (isset($createTimes) && $createTimes) {
            $userModel->whereTime("create_time", "between", $createTimes);
        }
        if (isset($keywords) && $keywords) {
            $userModel->whereLike("username|nickname|mobile", "%" . $keywords . "%");
        }

        $list = $userModel->paginate($pageSize, false, ['page' => $pageNum]);
        $total = $list->total();
        return $this->success(["list" => $list->items(), "total" => $total]);
    }

    public function getOptions(Request $request): Response
    {
        $dept = $request->get("deptId");
        $userModel = SysUser::where("status", 1);
        if ($dept) {
            $userModel->where("dept_id", $dept);
        }
        $list = $userModel->where("status", 1)->column(["id" => "value", "username" => "label"]);
        return $this->success($list);
    }

    public function setMyPassword(Request $request): Response
    {
        $jwtUser = $request->currUser;
        $password = $request->post("password");
        $user = SysUser::where('id', $jwtUser->userId)->find();
        if (!$user) {
            return $this->error('数据不存在');
        }
        $user->password = password_hash($password, PASSWORD_DEFAULT);
        if ($user->save()) {
            return $this->success([]);
        } else {
            return $this->error('更新失败');
        }
    }

    public function setMyProfile(Request $request): Response
    {
        $jwtUser = $request->currUser;
        $data = $request->post();
        $user = SysUser::where('id', $jwtUser->userId)->find();
        if (!$user) {
            return $this->error('数据不存在');
        }
        $user->update($data);
        return $this->success($user->toArray());
    }

    public function getMyProfile(Request $request): Response
    {
        $jwtUser = $request->currUser;
        $user = SysUser::where('id', $jwtUser->userId)->find();
        if (!$user) {
            return $this->error('数据不存在');
        }
        return $this->success($user->toArray());
    }

    public function getMobileCode(Request $request): Response
    {
        $mobile = $request->post("mobile");
        $code = rand(1000, 9999);
        return $this->success(["code" => $code]);
    }

    public function setMobile(Request $request): Response
    {
        $jwtUser = $request->currUser;
        $mobile = $request->post("mobile");
        $chkCode = $request->post("code");
        $user = SysUser::where('id', $jwtUser->userId)->find();
        if (!$user) {
            return $this->error('数据不存在');
        }
        $user->mobile = $mobile;
        if ($user->save()) {
            return $this->success($user->toArray());
        } else {
            return $this->error('更新失败');
        }
    }

    public function getEmailCode(Request $request): Response
    {
        $mobile = $request->post("mobile");
        $code = rand(1000, 9999);
        return $this->success(["code" => $code]);
    }

    public function setEmail(Request $request): Response
    {
        $jwtUser = $request->currUser;
        $email = $request->post("email");
        $chkCode = $request->post("code");
        $user = SysUser::where('id', $jwtUser->userId)->find();
        if (!$user) {
            return $this->error('数据不存在');
        }
        $user->email = $email;
        if ($user->save()) {
            return $this->success($user->toArray());
        } else {
            return $this->error('更新失败');
        }
    }

    public function setPassword(Request $request, int $userId): Response
    {
        $password = $request->get("password");
        $user = SysUser::find($userId);
        if (!$user) {
            return $this->error('数据不存在');
        }
        $user->password = password_hash($password, PASSWORD_DEFAULT);
        if ($user->save()) {
            return $this->success($user->toArray());
        } else {
            return $this->error('更新失败');
        }
    }

    public function setStatus(Request $request, int $userId): Response
    {
        $status = $request->post("status");
        $user = SysUser::find($userId);
        if (!$user) {
            return $this->error('数据不存在');
        }
        $user->status = $status;
        if ($user->save()) {
            return $this->success($user->toArray());
        } else {
            return $this->error('更新失败');
        }
    }
}
